package com.situ.shoplook.admin.api;

import com.situ.shoplook.admin.util.CannotDeleteNodeException;
import com.situ.shoplook.common.model.Category;
import com.situ.shoplook.common.model.Product;
import com.situ.shoplook.common.service.CategoryService;
import com.situ.shoplook.common.service.ProductService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/categories", produces = "application/json;charset=utf-8")
public class CategoryApi {
    @DubboReference
    private CategoryService categoryService;
    @DubboReference
    private ProductService productService;

    //查询全部
    @GetMapping
    public Map<String, Object> findAll() {
        List<Category> categories = this.categoryService.findAll();
        return Map.of("rows", categories, "success", true);
    }

    /**
     * 查询类别树
     *
     * @return 类别树
     */
    @GetMapping("/tree")
    public Map<String, Object> findTree() {
        Category category = categoryService.findTree();
//        System.out.println(category);
        return Map.of("data", category, "success", true);
    }

    @GetMapping("/id/{id}")
    public Map<String, Object> findById(@PathVariable(value = "id") Integer id) {
        System.out.println("id=" + id);
        Category category = this.categoryService.findById(id);
        if (category == null) {
            return Map.of("success", false, "error", "查询的类别不存在");
        }
        return Map.of("data", category, "success", true);
    }

    @GetMapping("/name")
    public Map<String, Object> findByName(@RequestParam(value = "name") String name) {
        System.out.println("name=" + name);
        Category category = null;
        Category cate = this.categoryService.findByName(name);
        if (cate.getParentId() != null) {
            category = this.categoryService.findByParentId(cate.getParentId());
            while (category.getParentId() != null) {
                category = this.categoryService.findByParentId(category.getParentId());
            }
            category = this.categoryService.findById(category.getId());
        } else {
            category = this.categoryService.findById(cate.getId());
        }
        if (category == null) {
            return Map.of("success", false, "error", "查询的类别不存在");
        }
        return Map.of("data", category, "success", true);
    }

    @PostMapping
    public Map<String, Object> save(@RequestBody Category category) {
        if (!StringUtils.hasText(category.getName())) {
            return Map.of("success", false, "error", "类别名称不可为空");
        }

        boolean success = this.categoryService.save(category);
        return Map.of("success", success);
    }

    @PutMapping
    public Map<String, Object> update(@RequestBody Category category) {
        boolean success = this.categoryService.update(category);
        return Map.of("success", success);
    }

    @DeleteMapping
    public Map<String, Object> deleteByIds(@RequestBody Integer[] ids) {
//        遍历所有id，判断是否有商品
        for (Integer id : ids) {
            List<Product> products = this.productService.findById(id);
            if (products.size() != 0) {
                return Map.of("success", false, "error", "该类别下有商品，无法删除");
            }
        }
        int rows = 0;
        try {
            rows = this.categoryService.deleteByIds(ids);
        } catch (CannotDeleteNodeException e) {
            return Map.of("success", false);
        }
        return Map.of("rows", rows, "success", rows > 0);
    }


}
